package com.atguigu.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.ware.dao.PurchaseDetailDao;
import com.atguigu.ware.entity.PurchaseDetailEntity;
import com.atguigu.ware.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        String status = (String) params.get("status");
        String wareId = (String) params.get("wareId");

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new LambdaQueryWrapper<PurchaseDetailEntity>().and(StrUtil.isNotEmpty(key), w ->
                        w.eq(PurchaseDetailEntity::getPurchaseId, key).or().eq(PurchaseDetailEntity::getSkuId, key))
                        .eq(StrUtil.isNotEmpty(status),PurchaseDetailEntity::getStatus,status)
                        .eq(StrUtil.isNotEmpty(wareId),PurchaseDetailEntity::getWareId,wareId)
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {

        return list(new LambdaQueryWrapper<PurchaseDetailEntity>().eq(PurchaseDetailEntity::getId, id));
    }

}